﻿using NPOI.Util;
using OSSUtility;
using OSSUtility.Communicate;
using OSSUtility.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ozone.Device
{

    /// <summary>
    /// 三工位设备  推杆 升降旋转夹爪
    /// </summary>
    public class DevThreeStationPushRod : DevBase
    {
        public DevThreeStationPushRod(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {

        }




        /// <summary>
        /// 初始化回到零点
        /// </summary>
        /// <returns></returns>
        public bool Init()
        {

            bool temperature = false;
            try
            {
                //01 05 00 11 00 00 9D CF
                //01 05 00 11 FF 00 DC 3F
                int nStartAddress = 17;
                Modbus modbus = new Modbus();
                ushort nStartAdr1 = ((ushort)nStartAddress);
                List<byte> bResult1 = new List<byte>();
                byte[] bytesData1 = new byte[] { 0x00, 0x00 };
                bResult1 = Modbus.writeFreameRTUSingle(AddrID, 0x05, nStartAdr1, bytesData1);
                byte[] byteRcv1 = session_.sendAndReadByteLock(bResult1.ToArray());
                Thread.Sleep(100);
                ushort nStartAdr2 = ((ushort)nStartAddress);
                List<byte> bResult2 = new List<byte>();
                byte[] bytesData2 = new byte[] { 0x00, 0xFF }; ;
                bResult2 = Modbus.writeFreameRTUSingle(AddrID, 0x05, nStartAdr2, bytesData2);
                byte[] byteRcv2 = session_.sendAndReadByteLock(bResult2.ToArray());
                if ((byteRcv1 != null) && (byteRcv2 != null))
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }
            return temperature;
        }

        /// <summary>
        /// 执行已配置的点位动作
        /// </summary>
        /// <returns></returns>
        public bool ExecuteNumberAction(int Number)
        {
            Number = 1000 + Number;
            bool temperature = false;
            try
            {
                //站点
                byte stationid = 0x01;

                //功能码
                byte funcode = 0x05;
                //起始地址
                ushort startAddr = (ushort)Number;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x00;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                //01 05 03 E8 00 00  4D BA
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null && byteRcv.Length == 8)
                {
                    Thread.Sleep(100);
                    bytesData[1] = 0xFF;
                    bytesData[0] = 0x00;
                    //01 05 03 E8 FF 00  0C 4A
                    returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                    byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                    if (byteRcv != null && byteRcv.Length == 8)
                    {
                        while (true)
                        {
                            Thread.Sleep(1000);
                            //速度为0代表执行完成
                            if (CurrentSpeed() == 0)
                            {
                                temperature = true;
                                break;
                            }
                            if (Number > 20)
                            {
                                temperature = false;
                                break;
                            }
                            Number++;
                        }

                    }
                }
                else
                {
                    temperature = false;
                }
            }
            catch (Exception ex)
            {
                temperature = false;
            }
            return temperature;
        }




        public int CurrentSpeed()
        {

            int temperature = 1;
            try
            {
                //站点
                byte stationid = 0x01;

                //功能码
                byte funcode = 0x04;
                //起始地址
                ushort startAddr = 0x02;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x02;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                //01 04 00 02 00 02 D0 0B 
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                //01 04 04 5A CB 3F 0B C8 95  （03D8 4220 转换浮点数 为 0.5443541mm/s
                if (byteRcv != null && byteRcv.Length == 9)
                {
                    temperature = 0;
                }

            }
            catch (Exception ex)
            {
            }
            return temperature;

        }
        /// <summary>
        /// 加瓶盖位置
        /// 开盖上升位置 此位置和夹爪
        /// </summary>
        /// <returns></returns>
        public bool ACC0()
        {

            bool temperature = false;
            try
            {
                //01 05 03 E8 00 00  4D BA
                //01 05 03 E8 FF 00  0C 4A
                //发送数据
                byte[] returnbytes = new byte[] {
                    0x01,0x05,0x03,0xE8,0x00,0x00,0x4D,0xBA                };
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                //收01 10 08 EC 00 02 82 5D
                if (byteRcv != null)
                {
                    temperature = true;
                }
                Thread.Sleep(100);

                returnbytes = new byte[] {
                    0x01,0x05,0x03,0xE8,0xFF,0x00,0x0C,0x4A            };
                byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());

                Thread.Sleep(2500);
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }



        /// <summary>
        /// 回原点
        /// </summary>
        /// <returns></returns>
        public bool ACC1()
        {

            bool temperature = false;


            try
            {
                //01 05 03 E9 00 00 1C 7A 
                //01 05 03 E9 FF 00 5D 8A
                //发送数据
                byte[] returnbytes = new byte[] {
                    0x01,0x05,0x03,0xE9,0x00,0x00,0x1C,0x7A                };
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                //收01 10 08 EC 00 02 82 5D
                if (byteRcv != null && byteRcv.Length == 8)
                {
                    temperature = true;
                }
                Thread.Sleep(100);

                returnbytes = new byte[] {
                     0x01,0x05,0x03,0xE9,0xFF,0x00,0x5D,0x8A                 };
                byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                //收01 10 08 EC 00 02 82 5D
                if (byteRcv != null && byteRcv.Length == 8)
                {
                    temperature = true;
                }

                Thread.Sleep(3000);
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }





        /// <summary>
        /// 开盖上抬
        /// </summary>
        /// <returns></returns>
        public bool ACC2()
        {

            bool temperature = false;


            try
            {
                //01 05 03 EA 00 00 EC 7A 
                //01 05 03 EA FF 00 AD 8A
                //发送数据
                byte[] returnbytes = new byte[] {
                    0x01,0x05,0x03,0xEA,0x00,0x00,0xEC,0x7A                };
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                //收01 10 08 EC 00 02 82 5D
                if (byteRcv != null)
                {
                    temperature = true;
                }
                Thread.Sleep(100);

                returnbytes = new byte[] {
                     0x01,0x05,0x03,0xEA,0xFF,0x00,0xAD,0x8A                 };
                byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                //收01 10 08 EC 00 02 82 5D
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }

        /// <summary>
        /// 关瓶盖下降位置
        /// </summary>
        /// <returns></returns>
        public bool ACC4()
        {

            bool temperature = false;


            try
            {
                //01 05 03 EC 00 00 0C 7B 
                //01 05 03 EC FF 00 4D 8B
                //发送数据
                byte[] returnbytes = new byte[] {
                    0x01,0x05,0x03,0xEC,0x00,0x00,0x0C,0x7B                };
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                //收01 10 08 EC 00 02 82 5D
                if (byteRcv != null)
                {
                    temperature = true;
                }
                Thread.Sleep(100);

                returnbytes = new byte[] {
                     0x01,0x05,0x03,0xEC,0xFF,0x00,0x4D,0x8B                 };
                byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                //收01 10 08 EC 00 02 82 5D
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }

        /// <summary>
        /// 关瓶盖下降
        /// </summary>
        /// <returns></returns>
        public bool ACC5()
        {

            bool temperature = false;


            try
            {
                //01 05 03 ED 00 00 5D BB 
                //01 05 03 ED FF 00 1C 4B
                //发送数据
                byte[] returnbytes = new byte[] {
                    0x01,0x05,0x03,0xED,0x00,0x00,0x5D,0xBB                };
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                //收01 10 08 EC 00 02 82 5D
                if (byteRcv != null)
                {
                    temperature = true;
                }
                Thread.Sleep(100);

                returnbytes = new byte[] {
                     0x01,0x05,0x03,0xED,0xFF,0x00,0x1C,0x4B                 };
                byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                //收01 10 08 EC 00 02 82 5D
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }


        /// <summary>
        /// 安装瓶盖
        /// </summary>
        /// <returns></returns>
        public bool Installbottlecap()
        {

            bool temperature = false;


            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x01;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }

        /// <summary>
        /// 卸瓶盖
        /// </summary>
        /// <returns></returns>
        public bool UnInstallbottlecap()
        {

            bool temperature = false;


            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x01;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }





        /// <summary>
        /// 补液
        /// </summary>
        /// <returns></returns>
        public bool FluidInfusion()
        {

            bool temperature = false;


            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x01;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }


        /// <summary>
        /// 获取三工位运行状态  
        /// true  运行中 
        /// false  停止状态
        /// </summary>
        /// <returns></returns>
        public bool GetThreeStationRunState()
        {

            bool temperature = false;


            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x01;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }


        /// <summary>
        /// 馏分清洗
        /// </summary>
        /// <returns></returns>
        public bool CollectCleaning()
        {

            bool temperature = false;


            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x01;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }















        byte AddrID = 01;


        /// <summary>
        /// 推杆移动 根据位置编号移动
        /// </summary>
        /// <param name="Pos">位置编号</param>
        /// <returns></returns>
        public bool PushMove(int Pos)
        {
            bool temperature = false;
            try
            {
                //0:到底38；1:回零；2-10->29-37,//
                //11->34.84
                //12->34
                //13->34.2
                //14->34.4
                //15->34.5
                //16->34.7
                //7->33.5
                //8->35
                int nStartAddress = 1000 + Pos;
                //发送数据
                //03 05 03 E9 00 00 1D 98
                //03 05 03 E9 FF 00 5C 68
                if (Pos < 0 || Pos > 16)
                    return temperature;
                Modbus modbus = new Modbus();
                ushort nStartAdr1 = ((ushort)nStartAddress);
                List<byte> bResult1 = new List<byte>();
                byte[] bytesData1 = new byte[] { 0x00, 0x00 }; ;
                bResult1 = Modbus.writeFreameRTUSingle(AddrID, 0x05, nStartAdr1, bytesData1);
                byte[] byteRcv1 = session_.sendAndReadByteLock(bResult1.ToArray());
                Thread.Sleep(100);
                ushort nStartAdr2 = ((ushort)nStartAddress);
                List<byte> bResult2 = new List<byte>();
                byte[] bytesData2 = new byte[] { 0x00, 0xFF }; ;
                bResult2 = Modbus.writeFreameRTUSingle(AddrID, 0x05, nStartAdr2, bytesData2);
                byte[] byteRcv2 = session_.sendAndReadByteLock(bResult2.ToArray());
                if ((byteRcv1 != null) && (byteRcv2 != null))
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }
            return temperature;
        }
        /// <summary>
        /// 推杆运动
        /// </summary>
        /// <param name="Pos">绝对位置</param>
        /// <param name="nSpeed">速度</param>
        /// <param name="nRate">加速度</param>
        /// <param name="nF">力矩</param>
        /// <returns></returns>
        public bool PushMovePos(float Pos, float nSpeed, float nRate, float nF)
        {
            bool temperature = false;
            try
            {
                //发送数据
                //01 10 08 EE 00 02 04 00 00 41 A0 2B 83 速度
                //01 10 08 F0 00 02 04 00 00 42 C8 AA 1D 加速度
                //01 10 08 F2 00 02 04 00 00 3F 00 0B 02 力矩
                //01 10 08 EC 00 02 04 00 00 41 A0 AA 5A 位置
                //速度
                int nStartAddressSpeed = 2286;
                byte[] bytesDataSpeed = Utility.floatToBytes(nSpeed);
                byte highByteSpeed = bytesDataSpeed[3];
                byte lowByteSpeed = bytesDataSpeed[2];
                List<byte> bResult = new List<byte>();
                ushort nStartAdr1 = ((ushort)nStartAddressSpeed);
                List<byte> bResult1 = new List<byte>();
                byte[] bytesData1 = new byte[] { lowByteSpeed, highByteSpeed, 0X00, 0X00, 0X04, 0X02, 0X00 };
                bResult1 = Modbus.writeFreameRTUSingle(AddrID, 0x10, nStartAdr1, bytesData1);
                byte[] byteRcv1 = session_.sendAndReadByteLock(bResult1.ToArray());
                Thread.Sleep(100);
                //加速度
                int nStartAddressRate = 2288;
                byte[] bytesDataRate = Utility.floatToBytes(nRate);
                byte highByteRate = bytesDataRate[3];
                byte lowByteSRate = bytesDataRate[2];
                ushort nStartAdr2 = ((ushort)nStartAddressRate);
                List<byte> bResult2 = new List<byte>();
                byte[] bytesData2 = new byte[] { lowByteSRate, highByteRate, 0X00, 0X00, 0X04, 0X02, 0X00 };
                bResult2 = Modbus.writeFreameRTUSingle(AddrID, 0x10, nStartAdr2, bytesData2);
                byte[] byteRcv2 = session_.sendAndReadByteLock(bResult2.ToArray());
                Thread.Sleep(100);
                //力矩
                int nStartAddressF = 2290;
                byte[] bytesDataF = Utility.floatToBytes(nF);
                byte highByteF = bytesDataF[3];
                byte lowByteF = bytesDataF[2];
                Modbus modbus = new Modbus();
                ushort nStartAdr3 = ((ushort)nStartAddressF);
                List<byte> bResult3 = new List<byte>();
                byte[] bytesData3 = new byte[] { lowByteF, highByteF, 0X00, 0X00, 0X04, 0X02, 0X00 };
                bResult3 = Modbus.writeFreameRTUSingle(AddrID, 0x10, nStartAdr3, bytesData3);
                byte[] byteRcv3 = session_.sendAndReadByteLock(bResult3.ToArray());
                Thread.Sleep(100);
                //位置
                int nStartAddressPos = 2284;
                byte[] bytesDataPos = Utility.floatToBytes(Pos);
                byte highBytePos = bytesDataPos[3];
                byte lowByteSPos = bytesDataPos[2];
                ushort nStartAdr4 = ((ushort)nStartAddressPos);
                List<byte> bResult4 = new List<byte>();
                byte[] bytesData4 = new byte[] { lowByteSPos, highBytePos, 0X00, 0X00, 0X04, 0X02, 0X00 };
                bResult4 = Modbus.writeFreameRTUSingle(AddrID, 0x10, nStartAdr4, bytesData4);
                byte[] byteRcv4 = session_.sendAndReadByteLock(bResult4.ToArray());
                if ((byteRcv1 != null) && (byteRcv2 != null) && (byteRcv3 != null) && (byteRcv4 != null))
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }
            return temperature;
        }
        public bool GetStatusPush()
        {
            bool IsActionComplete = false;
            try
            {
                //发送数据
                //03 02 00 08 00 01 39 EA
                //返回:03 02 01 01 61 F0
                Modbus modbus = new Modbus();
                ushort nStartAdr = ((ushort)8);
                List<byte> bResult = new List<byte>();
                byte[] bytesData = new byte[] { 0x01, 0x00 };
                bResult = Modbus.writeFreameRTUSingle(AddrID, 0x02, nStartAdr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(bResult.ToArray());
                if (byteRcv != null)
                {
                    if ((byteRcv[2] == 0X01) && (byteRcv[3] == 0X01))
                    {
                        IsActionComplete = true;
                    }
                }
            }
            catch (Exception ex)
            {
                IsActionComplete = false;
                throw;
            }
            return IsActionComplete;
        }






    }
}
